package gestioninscription;

import java.awt.Window;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;
import sql.GestionBdd;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author charlie
 */
public class FenDetailSession extends javax.swing.JFrame {
 
    Connection conn;
    Statement stmt, stmt1;
    ResultSet rs;
    
    private String num_session;
    /**
     * Creates new form FenDetailSession
     */
    public FenDetailSession(String num) {
        this.num_session = num;
        initComponents();
    }

    public FenDetailSession() {
        initComponents();
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Arial", 0, 36)); // NOI18N
        jLabel1.setText("Detail Session");

        jTable1.setModel(new ModeleJTableDetailSession());
        jScrollPane1.setViewportView(jTable1);

        jLabel2.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N
        jLabel2.setText("Participants provisoires");

        jTable2.setModel(new ModeleJTableListClient());
        jScrollPane2.setViewportView(jTable2);

        jLabel3.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N
        jLabel3.setText("Marge dégagée par la formation :");

        jTextField1.setFocusable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 711, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 406, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(20, 20, 20)
                                .addComponent(jLabel1))
                            .addComponent(jLabel2))
                        .addGap(29, 29, 29)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );

        jLabel1.getAccessibleContext().setAccessibleName("lbl1");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
       String pilote = "org.gjt.mm.mysql.Driver";
	String url = "jdbc:mysql://127.0.0.1/formarmor";
        
        try
	{
            Class.forName(pilote);           			            
            conn = DriverManager.getConnection(url,"root","");
            stmt = conn.createStatement();
            stmt = GestionBdd.connexionBdd(GestionBdd.TYPE_MYSQL, "formarmor","localhost", "root","");

            renseigneJtable();
            
            stmt1 = conn.createStatement();
            stmt1 = GestionBdd.connexionBdd(GestionBdd.TYPE_MYSQL, "formarmor","localhost", "root","");
            
            renseigneJtable1();
             
        }
        catch (SQLException se)
        {
            System.out.println("Erreur SQL1 : " + se.getMessage());
        }
        catch(ClassNotFoundException ce)
        {
           System.out.println("Erreur classe : " + ce.getMessage());
        }
       
    }//GEN-LAST:event_formWindowOpened

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FenDetailSession.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FenDetailSession.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FenDetailSession.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FenDetailSession.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FenDetailSession().setVisible(true);
            }
        });
    }
    
     private void renseigneJtable()
    {
        try
        {
            rs = stmt.executeQuery("select numero, libelleform, niveauform, datedebut, nb_places, nb_inscrits, nb_absents from session_form where numero = "+num_session);
            int ligne = 0, col;
            Object tauxRemplissage;

            for(int i=0;i<jTable1.getRowCount();i++)
            {
                for (col=0;col<jTable1.getColumnCount();col++)
                {
                        jTable1.setValueAt(null,i,col);
                }
            }
            while(rs.next())
            {
                for(col=0; col < jTable1.getColumnCount() -1 ; col++)
                {
                    jTable1.setValueAt(rs.getObject(col +1), ligne, col);
                }
                DecimalFormat df = new DecimalFormat("###.##");
                tauxRemplissage = (((Float.parseFloat(rs.getObject("nb_inscrits").toString()))-Float.parseFloat(rs.getObject("nb_absents").toString())) * 100) / Float.parseFloat(rs.getObject("nb_places").toString());
                jTable1.setValueAt(df.format(tauxRemplissage)+" %", ligne, 7);
                ligne++;
            }
            rs.close();
        }
        catch (SQLException SQLE)
        {
            System.out.println("Erreur SQL: " + SQLE.getMessage());
        }
        
    }
     
     private void renseigneJtable1()
    {
        try
        {
            rs = stmt.executeQuery( "SELECT distinct c.matricule, c.nom, st.taux_horaire, f.duree, f.coutrevient\n" +
                "FROM client c, session_form s, inscription i, statut st, formation f\n" +
                "WHERE s.numero = i.num_session\n" +
                "and i.matricule = c.matricule\n" +
                "and c.typestatut = st.type\n"+
                "and s.libelleform = f.libelle and s.niveauform = f.niveau\n"+
                "and s.numero = "+num_session);
            
            int ligne = 0, col;
            float marge = 0, coutrevient = 0;
            for(int i=0;i<jTable2.getRowCount();i++)
            {
                for (col=0;col<jTable2.getColumnCount();col++)
                {
                        jTable2.setValueAt(null,i,col);
                }
            }
            while(rs.next())
            {
                for(col=0; col < jTable2.getColumnCount() ; col++)
                {
                    jTable2.setValueAt(rs.getObject(col +1), ligne, col);
                }
                coutrevient = Float.parseFloat(rs.getString("coutrevient"));
                marge += Float.parseFloat(rs.getString("taux_horaire")) * Float.parseFloat(rs.getString("duree")) ;
                ligne++;
            }
            
            rs.close();
            marge -= coutrevient;
//            System.out.print(marge);
            jTextField1.setText(marge+" €");
            
        }
        catch (SQLException SQLE)
        {
            System.out.println("Erreur SQL: " + SQLE.getMessage());
        }
        
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
